package com.d6game.web.util;

import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientUtil {
	private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); 
	private static final int PROXY_PORT = 8087;
	private static final String PROXY_IP = "192.168.0.215";

	/**
	 * Http Get
	 * 
	 * @param netUrl
	 * @param isNeedProxy
	 * @return response string
	 */
	public static String doGet(String netUrl, boolean isNeedProxy) {
		String responseBody = null;
		HttpClient httpClient = new HttpClient();
		if (isNeedProxy) {
			httpClient.getHostConfiguration().setProxy(PROXY_IP, PROXY_PORT);
		}
		httpClient.getParams().setParameter(
				HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		// 浣跨敤GET鏂规硶锛屽鏋滄湇鍔″櫒闇�閫氳繃HTTPS杩炴帴锛岄偅鍙渶瑕佸皢涓嬮潰URL涓殑http鎹㈡垚https
		logger.info(" doGet connect:" + netUrl);
		GetMethod getMethod = new GetMethod(netUrl);
		// getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
		// new DefaultHttpMethodRetryHandler());

		int statusCode;
		try {
			statusCode = httpClient.executeMethod(getMethod);

			if (statusCode != HttpStatus.SC_OK) {
				System.err
						.println("Method failed:" + getMethod.getStatusLine());
			}
			responseBody = getMethod.getResponseBodyAsString();
			System.out.println("responseBody:"+responseBody);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return responseBody;
	}
	/**
	 * Http Post
	 * @param netUrl
	 * @param parameters
	 * @return response string
	 */
	public static String doPost(String netUrl, NameValuePair[] parameters, int timeout) {
		return doPost(netUrl, parameters, false, timeout);
	}

	public static String doPost(String netUrl, NameValuePair[] parameters,
			boolean isNeedProxy, int timeout) {
		String responseBody = null;
		MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        connectionManager.closeIdleConnections(100);
//        int timeout = 10; //milliseconds
//        connectionManager.getParams().setConnectionTimeout(timeout); //璁剧疆杩炴帴瓒呮椂锛屼竴鑸緢闅句細瓒呮椂鐨勶紝闄ら潪缃戠粶鐗瑰埆涓嶅ソ鎴栬�url缁欓敊浜�
        connectionManager.getParams().setSoTimeout(timeout);  //璁剧疆鑾峰彇鍒拌繑鍥炴暟鎹殑瓒呮椂锛屽嵆鏌ヨ瓒呮椂
		HttpClient httpClient = new HttpClient(connectionManager);
		if (isNeedProxy) {
			httpClient.getHostConfiguration().setProxy(PROXY_IP, PROXY_PORT);
		}
		httpClient.getParams().setParameter(
				HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		long start = System.currentTimeMillis();
		PostMethod postMethod = new PostMethod(netUrl);
		if (parameters != null)
			postMethod.setRequestBody(parameters);

		int statusCode;
		try {
//			postMethod.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
//			postMethod.setRequestHeader("Content-Type", "text/html; charset=ISO-8859-1");
			System.out.println(postMethod.getPath()+"|"+postMethod.getRequestCharSet()+"|"+postMethod.getName()+"|"+postMethod.getRequestEntity());
			
			statusCode = httpClient.executeMethod(postMethod);

			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("HttpClientUtil:Method failed:"
						+ postMethod.getStatusLine());
			}
			responseBody = postMethod.getResponseBodyAsString();
			System.out.println(System.currentTimeMillis() - start
					+ " ResponseBody:\n" + responseBody);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return responseBody;
	}

	public static String doPost(String netUrl, String queryString) {
		return doPost(netUrl, queryString, false);
	}
	
	public static String doPost(String netUrl, String queryString, boolean isNeedProxy){
		String responseBody = null;
		HttpClient httpClient = new HttpClient();
		if (isNeedProxy) {
			httpClient.getHostConfiguration().setProxy(PROXY_IP, PROXY_PORT);
		}
		httpClient.getParams().setParameter(
				HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
		long start = System.currentTimeMillis();
		PostMethod postMethod = new PostMethod(netUrl);
		if (queryString != null){
			postMethod.setQueryString(queryString);
		}
		int statusCode;
		try {
			statusCode = httpClient.executeMethod(postMethod);

			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed:"
						+ postMethod.getStatusLine());
			}
			responseBody = postMethod.getResponseBodyAsString();
			System.out.println(System.currentTimeMillis() - start
					+ " ResponseBody:\n" + responseBody);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return responseBody;
	}
}
